﻿using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using DotNetARX;

namespace AutoCADPlugin
{
    class 几何计算练习
    {
        [CommandMethod("GeoCal")]
        public void GeoCal()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            var shangDi = ed.GetDouble("\n请输入梯形的上底长度（米）");
            var xiaDi = ed.GetDouble("\n请输入梯形的下底长度（米）");
            var mianJi = ed.GetDouble("\n请输入梯形的面积（平方米）");
            double a = shangDi.Value;
            double b = xiaDi.Value;
            double S = mianJi.Value;
            double h = 2 * S / (a + b);

            //确定A点位置
            var ppr = ed.GetPoint(new PromptPointOptions("\n请确定A点位置"));
            if (ppr.Status == PromptStatus.OK)
            {
                Point3d AAA = ppr.Value;
                double AX = ppr.Value.X;
                double AY = ppr.Value.Y;
                Point2d AA = new Point2d(AX, AY);


                //输入∠BAD的度数
                var pao = new PromptAngleOptions("\n请输入一个角度");
                pao.BasePoint = AAA;
                pao.UseBasePoint = true;
                var par = ed.GetAngle(pao);
                if (par.Status == PromptStatus.OK)
                {
                    double angle = par.Value;
                    double tanValue = Math.Tan(Math.PI * angle / 180);
                    double BX = h / tanValue;
                    Point2d BB = new Point2d(BX, h);
                    Point2d CC = new Point2d(BX + a, h);
                    Point2d DD = new Point2d(AX + b, 0);
                    var pl = new Polyline();
                    pl.CreatePolyline(AA, BB, CC, DD);
                    pl.Closed = true;
                }
            }

        }
    }
}
